package milkyway.hightech.entry;

import java.math.BigDecimal;

import java.text.DateFormat;

import java.util.Date;

import milkyway.hightech.money.Money;
import milkyway.hightech.timer.TimeEmitter;


/**
 *
 * @author michael
 */
public final class Entry
{
    private final Date date;
    private final String dateAsString;

    /**
     * the amount of money
     */
    private final Money money;

    /**
     * the description of this transfer
     */
    private final String reasonOfTransfer;
    private final char sign;

    /**
     *
     * @param reasonOfTransfer
     * @param sign
     * @param money
     */
    public Entry(String reasonOfTransfer, char sign, Money money)
    {
        this.money = money;
        this.reasonOfTransfer = toFixedString(reasonOfTransfer, 30);
        this.sign = sign;

        DateFormat dateFormatter = DateFormat.getDateInstance();
        DateFormat timeFormatter = DateFormat.getTimeInstance();

        date = TimeEmitter.getInstance().getCalendar().getTime();
        dateAsString = dateFormatter.format(date) + ", "
                       + timeFormatter.format(date) + " Uhr";
    }

    /**
     * trims the given string into the given size.
     * @param string
     * @param size
     * @return
     */
    private String toFixedString(String string, int size)
    {
        char[] reason = new char[size];

        for (int i = 0; i < reason.length; i++)
        {
            reason[i] = ' ';
        }

        if (string.toCharArray().length > reason.length)
        {
            System.arraycopy(string.toCharArray(), 0, reason, 0,
                             reason.length);
        } else
        {
            System.arraycopy(string.toCharArray(), 0, reason, 0,
                             string.toCharArray().length);
        }

        return String.valueOf(reason);
    }

    /**
     * gets a string-object of this transfer.
     * @return
     */
    @Override
    public String toString()
    {
        BigDecimal d =
            new BigDecimal(money.getValue()).divide(BigDecimal.valueOf(100));
        
        String data = "Reason of transfer: " + reasonOfTransfer + "\tamount: "
                      + sign + d + " " + money.getCurrency()
                      + "\tdate of transfer: " + dateAsString + "\n";

        return data;
    }

    /**
     *
     * @return
     */
    public Date getDate()
    {
        return date;
    }

    /**
     *
     * @return
     */
    public String getDateAsString()
    {
        return dateAsString;
    }

    /**
     *
     * @return
     */
    public BigDecimal getAmount()
    {
        BigDecimal d =
            new BigDecimal(money.getValue()).divide(BigDecimal.valueOf(100));

        return d;
    }

    /**
     *
     * @return
     */
    public Money getMoney()
    {
        return money;
    }

    /**
     *
     * @return
     */
    public String getReasonOfTransfer()
    {
        return reasonOfTransfer;
    }

    /**
     *
     * @return
     */
    public char getSign()
    {
        return sign;
    }
}
